/*
 * @Author: ChunLai
 * @Date: 2023-08-08 08:53:31
 * @LastEditTime: 2025-09-05 08:54:21
 * @Description:银行卡签约流程
 * @FilePath: \02.bldinsure\src\views\pay\banksign.js
 */
import {
  paymentAuthQuery,
  paymentAuthApply,
  paymentAuthConfirm,
} from "@/utils/functions.js";
import { rebackUrlPath } from "@/methods/format.js";

export const bankCardSignHandleMixin = {
  data() {
    return {
      transactionId: "", //交易串号
      authTransrNo: "", //序列号
      signConfirmType: "", //签约确认方式
      signType: "01", //签约类型，01签约，02解约
      signFlag: "-1",
    };
  },
  computed: {
    signText() {
      let res = "签约查询";
      if (this.signFlag == "1") {
        res = "签约申请";
      } else if (this.signFlag == "2") {
        res = "签约确认";
      } else if (this.signFlag == "4") {
        res = "去解约";
      } else if (this.signFlag == "5") {
        res = "解约确认";
      }
      return res;
    },
  },
  methods: {
    signBeforeQuery(type = "") {
      this.$store.commit("updateLoad", true);
      paymentAuthQuery(this.apiComParams).then(
        ({ data }) => {
          this.$store.commit("updateLoad", false);
          this.handleCode(data.code, data.msg, data, type);
        },
        (err) => {
          this.$store.commit("updateLoad", false);
        }
      );
    },
    // 申请
    payAuthApplyApi(type) {
      this.signType = type;
      let rePath = `#/pay`;
      if (this.bankCardInfo.payMode == "7") {
        rePath = `#/billres`;
      }
      this.$store.commit("updateLoad", true);
      let params = {
        ...this.apiComParams,
        transactionId: this.transactionId,
        authTransrNo: this.authTransrNo,
        signType: this.signType,
        successUrl: `${rebackUrlPath(`${rePath}?signres=1`)}`,
        failUrl: `${rebackUrlPath(`${rePath}?signres=2`)}`,
      };
      paymentAuthApply(params).then(
        ({ data }) => {
          this.$store.commit("updateLoad", false);
          this.handleCode(data.code, data.msg, data);
        },
        (err) => {
          this.$store.commit("updateLoad", false);
        }
      );
    },
    // 确认
    payAuthSureApi(type = "", smsCode = "") {
      if (type) {
        this.signType = type;
      }
      this.$store.commit("updateLoad", true);
      let params = {
        ...this.apiComParams,
        transactionId: this.transactionId,
        authTransrNo: this.authTransrNo,
        signType: this.signType,
        smsCode: smsCode, //手机收到的短信验证码
      };
      paymentAuthConfirm(params).then(
        ({ data }) => {
          this.$store.commit("updateLoad", false);
          this.handleCode(data.code, data.msg, data);
        },
        (err) => {
          this.$store.commit("updateLoad", false);
        }
      );
    },
    handleCode(apiCode, apiMsg = "", apiRes, type = "") {
      if (apiRes.transactionId) {
        this.transactionId = apiRes.transactionId;
      }
      if (apiRes.authTransrNo) {
        this.authTransrNo = apiRes.authTransrNo;
      }
      if (apiRes.signConfirmType) {
        this.signConfirmType = apiRes.signConfirmType;
      }
      if (apiRes.url) {
        this.signUrl = apiRes.url;
      }
      this.signFlag = apiCode;
      if (type === "init") {
        return;
      }
      if (apiCode === "3") {
        this.$baseAlert(apiMsg, () => {
          // this.toPayPaiHandle();
        });
      } else if (["0", "6"].includes(apiCode)) {
        // 换卡提示
        this.$baseConfirm(
          `${apiMsg || "签约失败"}<br>是否需要更换银行卡?`,
          () => {
            this.$router.push("/replace");
          }
        );
      } else {
        let sureTxt = `${
          apiCode == "1"
            ? "签约申请"
            : apiCode == "2"
            ? "签约确认"
            : apiCode == "4"
            ? "去解约" // 调解约申请
            : apiCode == "5"
            ? "解约确认" // 调解约确认
            : ""
        }`;
        this.$baseConfirm(
          `${apiMsg}`,
          () => {
            this.signCodeHandle(apiCode);
          },
          () => {
            // this.$router.push('/replace');
          },
          `${sureTxt}`
        );
      }
    },
    signCodeHandle(apiCode) {
      if (apiCode == "1") {
        this.payAuthApplyApi("01");
      } else if (apiCode == "2") {
        this.signType = "01";
        // 签约确认方式，1短信验证码 ，2短信回复，3跳转链接(此类型会返回url字段)
        if (this.signConfirmType == "1") {
          this.$refs.smsCodeRef.initModule(this.bankCardInfo.firBankMobile);
        } else if (this.signConfirmType == "2") {
          this.$msAlert("请尽快完成短信回复，查询签约确认")
        } else if (this.signConfirmType == "3") {
          window.location.href = this.signUrl;
        }
      } else if (apiCode == "4") {
        this.payAuthApplyApi("02");
      } else if (apiCode == "5") {
        this.payAuthSureApi("02");
      }
    },
  },
};
